package com.atguigu.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author 伟哥
 * @date 2021-06-03 7:41
 */
@Controller
public class ExceptionController {

    /**
     * 制造数学异常，交由本类处理
     * @return
     */
    @RequestMapping("/exception1")
    public String exceptionTest1(){
        int i = 1/0;
        return "success";
    }

    /**
     * 制造其他异常，交由{@link com.atguigu.controller.ExceptionCenterController#handleExceptionTest2(Exception 异常) ExceptionCenterController类的handleExceptionTest2(Exception 异常)方法}进行处理
     * @return
     */
    @RequestMapping("/exception2")
    public Exception exceptionTest2(){
        throw new RuntimeException();
    }

    //处理空指针，和数学异常
    @ExceptionHandler(value = { ArithmeticException.class,NullPointerException.class})
    public ModelAndView handleexceptionTest1(Exception exception ){
        System.out.println("handleException02被调用..." + exception);
        ModelAndView myError = new ModelAndView("myError");
        myError.addObject("ex",exception);

        // 视图解析器拼串
        return myError;
    }
}
